﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Subsonic.Data;
using MichMan.Utilities.Requests;
using MichMan.Utilities;

namespace Subsonic.ViewModels
{
    public class ArtistAlbumsViewModel : PivotViewModel
    {
        public ArtistAlbumsViewModel(string artistId, string selectedAlbum)
        {
            ArtistId = artistId;
            SelectedAlbum = selectedAlbum;
        }

        public string ArtistId { get; set; }
        public string SelectedAlbum { get; set; }

        public override IRequest LoadPivots()
        {
            GetMusicDirectoryRequest getMusicDirectoryRequest = new GetMusicDirectoryRequest(ArtistId);

            getMusicDirectoryRequest.RunRequestCompleted += new RunRequestCompletedEventHandler(musicDirReq_RunRequestCompleted);
            getMusicDirectoryRequest.RunRequestAsync();
            return getMusicDirectoryRequest;
        }

        void musicDirReq_RunRequestCompleted(object sender, RunRequestCompletedEventArgs e)
        {
            GetMusicDirectoryRequest getMusicDirectoryRequest = (GetMusicDirectoryRequest)sender;

            if (getMusicDirectoryRequest.Response == null || getMusicDirectoryRequest.RequestStatus != RequestStatus.Succeeded)
            {
                return;
            }

            foreach (var child in getMusicDirectoryRequest.Response.Directory.Children.Pivot(a => a.Id == SelectedAlbum))
            {
                if (child.IsDir)
                {
                    Pivots.Add(new AlbumViewModel(child));
                }
            }
        }
    }
}
